// 获取日期时间 [[yyyy, MM, dd], [HH, mm, ss]]
function dateTime(time) {
    var day = new Date()
    if (time) {
        day = new Date(time)
    }

    return [
        [day.getFullYear(), (day.getMonth()+1).Pad(), day.getDate().Pad()],
        [day.getHours().Pad(), day.getMinutes().Pad(), day.getSeconds().Pad()]
    ]
}

// 获取时间区间
function timeSection(time) {
    var day = new Date()
    if (time) {
        day = new Date(time)
    }

    var millisecond = 1000 * 60 * 60 * 24;

    var todayFirst = new Date(day.getFullYear(), day.getMonth(), day.getDate())
    var todayLast = new Date(todayFirst.getTime() + millisecond - 1)

    var monthFirst = new Date(day.getFullYear(), day.getMonth(), 1);
    var monthLast = new Date(day.getFullYear(), day.getMonth() + 1, 0, 23, 59, 59, 999);

    var week = todayFirst.getDay();
    var minusDay = week != 0 ? week - 1 : 6;
    var monday =new Date(todayFirst.getTime() - (minusDay * millisecond));
    var sunday =new Date(monday.getTime() + (7 * millisecond) - 1);

    var yearFirst = new Date(day.getFullYear(), 0, 1);
    var yearLast = new Date(day.getFullYear(), 12, 0, 23, 59, 59, 999);

    return {
        today: [todayFirst, todayLast],
        month: [monthFirst, monthLast],
        week: [monday, sunday],
        year: [yearFirst, yearLast],
    }
}

// 日期追加
Date.prototype.AddDate = function(year=0, month=0, day=0, hour=0, minute=0, second=0, millisecond=0) {
    return new Date(this.getFullYear()+year, this.getMonth()+month, this.getDate()+day, this.getHours()+hour, this.getMinutes()+minute, this.getSeconds()+second, this.getMilliseconds()+millisecond)
}

// 日期格式化
Date.prototype.Format = function(fmt="yyyy-MM-dd HH:mm:ss")   {  
  var o = {   
    "M+" : this.getMonth()+1,                 //月份   
    "d+" : this.getDate(),                    //日   
    "H+" : this.getHours(),                   //小时   
    "m+" : this.getMinutes(),                 //分   
    "s+" : this.getSeconds(),                 //秒   
    "q+" : Math.floor((this.getMonth()+3)/3), //季度   
    "S"  : this.getMilliseconds()             //毫秒   
  };

  if(/(y+)/.test(fmt))
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  for(var k in o)
    if(new RegExp("("+ k +")").test(fmt))

  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));

  return fmt;
}

// 数字前导0，默认2位
Number.prototype.Pad = function(size=2) {
    var s = String(this);
    while (s.length < (size || 2)) {s = "0" + s;}
    return s;
}

// 获取两个日期的差值 [天数, 格式化]
function diffDay(start, end) {
    var stime = (new Date(start)).getTime()
    var etime = (new Date(end)).getTime()
    var day = parseInt((etime - stime)/86400000)

    var aday = Math.abs(day)
    var format = ""
    if (aday >= 365) {
      format = format + parseInt(aday/365) + "Y"
      aday = aday % 365
    }

    if (aday >= 30) {
      format = format + parseInt(aday/30) + "M"
      aday = aday % 30
    }

    if (aday > 0) {
      format = format + aday + "D"
    }

    if (aday == Math.abs(day)) {
        format = format + "D"
    }
    
    return [day, format]
}

const restDay = [20210731,20210801,20210807,20210808,20210814,20210815,20210821,20210822,20210828,20210829,20210904,20210905,20210911,20210912,20210919,20210920,20210921,20210925,20211001,20211002,20211003,20211004,20211005,20211006,20211007,20211010,20211016,20211017,20211023,20211024,20211030,20211031,20211106,20211107,20211113,20211114,20211120,20211121,20211127,20211128,20211204,20211205,20211211,20211212,20211218,20211219,20211225,20211226]

// 上一个工作日（包含this）
Date.prototype.LastWorkDay = function() {
    var day = this
    var dayNum = parseInt(day.Format("yyyyMMdd"))
    for (var i in restDay) {
        if (restDay[i] > dayNum) {
            break
        }

        if (restDay[i] < dayNum) {
            continue
        }

        for(var j=i; j>=0; j--){
            if (restDay[j] != dayNum) {
                break
            }

            day = day.AddDate(0, 0, -1)
            dayNum = parseInt(day.Format("yyyyMMdd"))
        }
        break
    }

    return day
}

// 下一个工作日（包含this）
Date.prototype.NextWorkDay = function() {
    var day = this
    var dayNum = parseInt(day.Format("yyyyMMdd"))
    for (var i in restDay) {
        if (restDay[i] > dayNum) {
            break
        }

        if (restDay[i] < dayNum) {
            continue
        }

        for(var j=i; j<restDay.length; j++){
            if (restDay[j] != dayNum) {
                break
            }

            day = day.AddDate(0, 0, 1)
            dayNum = parseInt(day.Format("yyyyMMdd"))
        }
        break
    }

    return day
}